package chain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import LPU.FirstStepForLPU;
import LPU.LPUInTwoSteps;
import termWeighting.PrepareTF;
import type.TfType;
import util.BugListProcess;
import util.MapUtil;
import dataProcess.BugInfoToTextFile;
import dataProcess.ForLPU;
import dataProcess.ReadExcel;
import ds.BugInfo;
import ds.Constants;

public class RunLPUinChain {
	public ArrayList<BugInfo> allBugs;
	
	public RunLPUinChain(){
		allBugs = new ArrayList<BugInfo>();
	}
	
	public static void main(String[] args){
		RunLPUinChain rpc = new RunLPUinChain();
		rpc.getBugs();
		
		rpc.run("Functionality");
		rpc.run("Security");
		rpc.run("Usability");
		rpc.run("Performance");
	}
	
	public void getBugs() {
		/**categories*/
		String[] originCategories = { "Functionality", "Security", "Usability", "Performance",
				"Security Defect", "UI", "Efficiency"};
		HashMap<String, String> mergeMap = new HashMap<String, String>();
		mergeMap.put("UI", "Usability");
		mergeMap.put("Security Defect", "Security");
		mergeMap.put("Efficiency", "Performance");
		/**text indexes in Excel file*/
		int[] textIndexes1 = {10};
		int[] textIndexes2 = {1, 8};
		int[] textIndexes4 = {3, 4};
		/**different data resource*/
		ReadExcel bugGetter = new ReadExcel(originCategories);
		allBugs.addAll(bugGetter.getBugsFromXls("data/input/BugReports1.xls",  textIndexes1, 6));//MS 1
		allBugs.addAll(bugGetter.getBugsFromXls("data/input/BugReports2.xls", textIndexes2, 4));//MS 2
		allBugs.addAll(bugGetter.getBugsFromXls("data/input/BugReports4.xls", textIndexes4, 0));// Android 
		allBugs.addAll(bugGetter.getSecurityBugs("data/input/51security"));//Mozilla
		
		ArrayList<String> labelList = bugGetter.getLabels();
		labelList.removeAll(mergeMap.keySet());
		// merge same category
		BugListProcess blp = new BugListProcess(allBugs, labelList);
		blp.mergeCategory(mergeMap);
	}
	public void run(String category){
		ArrayList<ArrayList<BugInfo>> bugSet = ReadExcel.getPosAndNon(allBugs,category);
		BugInfoToTextFile.bugList2FileList(bugSet.get(0),
				"output/"+category+"Bugs/");
		BugInfoToTextFile.bugList2FileList(bugSet.get(1),
				"output/Non"+category+"Bugs/");

		try {
			PrepareTF.run("output/"+category+"Bugs/", "output/Non"+category+"Bugs/",
					"output/LPU/TfFile", "output/LPU/wordList", TfType.TFIDF);
			LPUInTwoSteps.run(MapUtil.getGroundTruth(allBugs),
					Constants.LPU_TfFile);
			System.out.print("LPU for "+category+" completed!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void runSameTest() throws Exception {
		// category info
		String[] originCategories = { "Functionality", "Security", "UI",
				"Usability", "Security Defect", "Performance", "" };
		HashMap<String, String> mergeMap = new HashMap<String, String>();
		mergeMap.put("UI", "Usability");
		mergeMap.put("Security Defect", "Security");
		// index info
		int[] textIndexes1 = { 10 };
		int[] textIndexes2 = { 1, 8 };

		// All bugs
		ReadExcel bugGetter = new ReadExcel(originCategories);
		ArrayList<BugInfo> allBugs = bugGetter.getBugsFromXls(
				"data/input/BugReports1.xls", textIndexes1, 6);
		allBugs.addAll(bugGetter.getBugsFromXls("data/input/BugReports2.xls",
				textIndexes2, 4));
		allBugs.addAll(bugGetter.getSecurityBugs("data/input/51security"));

		// All categories
		ArrayList<String> labelList = bugGetter.getLabels();
		labelList.removeAll(mergeMap.keySet());

		// merge same category, divide train and test
		BugListProcess blp = new BugListProcess(allBugs, labelList);
		blp.run(mergeMap, 0.3);
		// calculate TF for train and test
		PrepareTF.run(Constants.trainDirForLPU, Constants.testDirForLPU,
				"output/LPU/TfFile", "output/LPU/wordList", TfType.pureTF);

		for (String curLabel : labelList) {
			if (!curLabel.equals("")) {
				MapUtil.savePosId(Constants.posIDmap, curLabel, allBugs);
				try {
					double[] posRates = { 0.3, 0.4, 0.5, 0.6, 0.7 };
					ForLPU lpu = new ForLPU();
					// lpu.runWithOriginUnlabel();
					for (double posRate : posRates)
						lpu.runWithSameTest(curLabel + posRate, posRate);
					System.out.print("LPU for same test completed!");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

	}

}
